using System;
using System.Collections.Generic;
using System.Text;

namespace Lutos.Control.Misc
{
    public class RandomImage : System.Web.UI.WebControls.WebControl
    {
        private System.Collections.ArrayList image_list;
        private System.Collections.ArrayList priority_list;
        private int sum = 0;

        public RandomImage():base()
        {
            this.image_list = new System.Collections.ArrayList();
            this.priority_list = new System.Collections.ArrayList();
            sum = 0;
        }

        public int Insert(string path, int priority)
        {
            this.image_list.Add(path);
            this.priority_list.Add(sum + priority);
            sum = sum + priority;
            return image_list.Count;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            System.Text.StringBuilder sb = new StringBuilder();
            System.Random r = new Random();
            int index=r.Next(sum);
            int i=0;
            for(i=0;i<priority_list.Count;i++)
            {
                if (index <= ((int)priority_list[i]))
                {
                    break;
                }
            }
            //this.ImageUrl = (string)image_list[i];
            sb.AppendFormat("<img src=\"{0}\"></img>", image_list[i]);
            writer.Write(sb.ToString());
        }
    }
}
